package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.BinaryRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.TaskConstants;
import tech.abing.spider.task.model.amac.AttachmentEntity;
import tech.abing.spider.task.model.amac.BlacklistEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class BlackListDetailProcessor implements PageProcessor{
	
	private static Logger logger = Logger.getLogger(BlackListDetailProcessor.class);
	
	private static int record = 0;

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		Element table = doc.select("div.innerCenter > div.iRight > div.ldContent").first();
		Elements trs =table.select("tr:gt(0)");
		Element thead = table.select("tr:eq(0) > td:eq(1)").first();
		for (Element tr : trs) {
			record++;
			logger.info("正在解析第"+record+"条数据");
			BlacklistEntity blackList = new BlacklistEntity();
			blackList.setId(UuIdPublic.timeStrongID(5));
			blackList.setCreateTime(new Timestamp(System.currentTimeMillis()));
			String url = page.getRequest().getUrl();
			blackList.setUrl(url);
			Elements tds = tr.select("td");
			if(StringUtils.isNotEmpty(tds.get(0).text())){
				String disciplinaryDate = tds.get(0).text().replace(Jsoup.parse("&nbsp;").text(),"");
				if(StringUtils.isNotEmpty(disciplinaryDate)){
					blackList.setDisciplinaryDate(DateUtils.parseDate(disciplinaryDate));
				}
			}
			if("姓名".equals(thead.text())){
				blackList.setPersonalName(tds.get(1).text());
				if(StringUtils.isNotEmpty(tds.get(2).select("p:nth-child(1)").text())){
					blackList.setOrgName(tds.get(2).select("p:nth-child(1)").text());
					blackList.setPosition(tds.get(2).select("p:nth-child(2)").text());
				}else{
					blackList.setOrgName(tds.get(2).text());
				}
				blackList.setDisciplinaryName(tds.get(3).select("p").first().text());
			}else{
				blackList.setOrgName(tds.get(1).text());
				blackList.setDisciplinaryName(tds.get(2).select("p").first().text());
			}
			blackList.setAttachmentUrl(tds.select("a[filerela]").attr("href"));
			StringUtils.removeSpace(blackList);
			if(StringUtils.isNotEmpty(blackList.getAttachmentUrl())){
				AttachmentEntity attachment = new AttachmentEntity();
				String uuid= UuIdPublic.timeStrongID(5);
				attachment.setUrl(url);
				attachment.setAttachType(TaskConstants.TASK_BLACKLIST);
				attachment.setDownloadAddress(blackList.getAttachmentUrl());
				attachment.setFileName(UuIdPublic.timeStrongID(5));
				attachment.setDisplayName(tds.select("a[filerela]").text());
				String savePath = "doc/"+DateUtils.getDate("yyyyMMdd")+"/"+attachment.getAttachType()+"/";
				attachment.setSavePath(savePath);
				attachment.setFileType("pdf");
				attachment.setCreateTime(new Timestamp(System.currentTimeMillis()));
				attachment.setId(uuid);
				StringUtils.removeSpace(attachment);
				objectContainer.add(attachment);
				BinaryRequest req = context.createBinaryRequest(blackList.getAttachmentUrl(), AttachmentProcessor.class);
				req.addAttribute("attachment", attachment);
				
				queue.add(req);
			}
			objectContainer.add(blackList);
		}
		
	}


	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}
